/*
 *	Atom database
 *
 *	Copyright (C) 2011 XiaoJSoft Studio. All Rights Reserved.
 *	Copyright (C) Ji WenCong <whs_jwc@163.com>
 *
 *	kasprintf (copied & modified from Linux Kernel):
 *	Copyright (C) Linus Torvalds <torvalds@klaava.Helsinki.Fi>
 *
 *	This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdio.h>
#include <string.h>
#include "../../localization/local.h"
#include "../../include/console/utils/atom.h"

/*
 *	double getAtomHeavy(char *name, int *succeed);
 *
 *	to get the weight of a atom
 *
 */
double getAtomHeavy(char *name, int *succeed)
{
	int idx;
	*succeed = 0;
	for (idx = 0; idx <= chemical_ecount; idx++) {
		if ((strcmp(chemical_enames[idx], name) == 0) || (strcmp(chemical_enames_eng[idx], name) == 0)) {
			*succeed = 1;
			return(chemical_heavy[idx]);
		}
	}
	return(0);
}

/*
 *	double getAtomIonizationEnergy(char *name, int *succeed);
 *
 *	to get the ionization energy of a atom
 *
 */
double getAtomIonizationEnergy(char *name, int *succeed)
{
	int idx;
	*succeed = 0;
	for (idx = 0; idx <= chemical_ecount; idx++) {
		if ((strcmp(chemical_enames[idx], name) == 0) || (strcmp(chemical_enames_eng[idx], name) == 0)) {
			*succeed = 1;
			return(chemical_ionization_energy[idx]);
		}
	}
	return(0);
}

/*
 *	char* getAtomEnglishName(char *symbol);
 *
 *	to get the english name of a atom
 *
 */
char* getAtomEnglishName(char *symbol)
{
	int idx;
	for (idx = 0; idx <= chemical_ecount; idx++) {
		if ((strcmp(chemical_enames[idx], symbol) == 0) || (strcmp(chemical_enames_eng[idx], symbol) == 0)) {
			return(chemical_enames_eng[idx]);
		}
	}
	return(NULL);
}

/*
 *	char* getAtomGStateConf(char *symbol);
 *
 *	to get the gound-state configuration of a atom
 *
 */
char* getAtomGStateConf(char *symbol)
{
	int idx;
	for (idx = 0; idx <= chemical_ecount; idx++) {
		if ((strcmp(chemical_enames[idx], symbol) == 0) || (strcmp(chemical_enames_eng[idx], symbol) == 0)) {
			return(chemical_grstate_conf[idx]);
		}
	}
	return(NULL);
}

/*
 *	char* getAtomGStateLevel(char *symbol);
 *
 *	to get the gound-state level of a atom
 *
 */
char* getAtomGStateLevel(char *symbol)
{
	int idx;
	for (idx = 0; idx <= chemical_ecount; idx++) {
		if ((strcmp(chemical_enames[idx], symbol) == 0) || (strcmp(chemical_enames_eng[idx], symbol) == 0)) {
			return(chemical_grstate_level[idx]);
		}
	}
	return(NULL);
}

/*
 *	int atomExist(char *symbol);
 *
 *	to whether the atom exists
 *
 */
int atomExist(char *symbol)
{
	int idx;
	for (idx = 0; idx <= chemical_ecount; idx++) {
		if ((strcmp(chemical_enames[idx], symbol) == 0) || (strcmp(chemical_enames_eng[idx], symbol) == 0)) {
			return(idx);
		}
	}
	return(-1);
}

/*
 *	void listElements(int from, int to);
 *
 *	to show the periodic table
 *
 */
void listElements(int from, int to)
{
	int idx;
	printf(localization_table[55]);
	for (idx = (from < 0 ? 0 : from); idx <= (to > chemical_ecount ? chemical_ecount : to); idx++) {
		if (chemical_ionization_energy[idx] <= 0) {
			printf("%d\t%s\t%f\tN/A\n", idx, chemical_enames[idx], chemical_enames_eng[idx], (double) chemical_heavy[idx]);
		} else {
			printf("%d\t%s\t%f\t%f\n", idx, chemical_enames[idx], chemical_enames_eng[idx], (double) chemical_heavy[idx], (double) chemical_ionization_energy[idx]);
		}
	}
}

/*
 *	void traceElementInfo(int idx, char *ename, char *ename_eng, double weight, double ev, char *grconf, char *grlevel);
 *
 *	to print the atom's detail information on the screen
 *
 */
void traceElementInfo(int idx, char *ename, char *ename_eng, double weight, double ev, char *grconf, char *grlevel)
{
	printf(localization_table[56], idx);
	printf(localization_table[57]);
	printf(localization_table[58], ename);
	printf(localization_table[59], ename_eng);
	printf(localization_table[60], weight);
	printf(localization_table[61], ev);
	printf(localization_table[62], grconf);
	printf(localization_table[63], grlevel);	
}

